package com.qdsgjsfk.vision.ui;

import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.qdsgjsfk.vision.R;
import com.rest.business.RestProxy;
import com.rest.response.CollectStudentResponse;
import com.rest.response.Student;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.qdsgjsfk.vision.base.BaseActivity;
import com.qdsgjsfk.vision.util.ImageLoaderHelper;
import com.qdsgjsfk.vision.util.NetUtil;

/**
 * Created by HP on 2019/11/28.
 */

public class FaceCheckActivity extends BaseActivity {
    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.btn_next)
    TextView btn_next;


    private GestureDetector gd;
    private String classId;
    private List<Student> list;
    private int currentPosition;
    private int type;
    private String faceImg;
    private String studentId,studentName,taskId;
    private int verticalMinDistance = 20;
    private int minVelocity = 0;

    @Override
    protected void initViews() {
        gd = new GestureDetector( FaceCheckActivity.this, listener);
        if (type == 1) {
            btn_next.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    protected void initData() {
        classId = getIntent().getStringExtra("classId");
        type = getIntent().getIntExtra("type", 0);
        faceImg = getIntent().getStringExtra("faceImg");
        studentName =getIntent().getStringExtra("studentName");
        taskId =getIntent().getStringExtra("taskId");
        studentId = getIntent().getStringExtra("studentId");
        if (type == 0) {
            getStudentFace();
        } else {
            ImageLoaderHelper.getInstance().GlideImageLoader( FaceCheckActivity.this, faceImg, iv);
            tv_name.setText(studentName);
        }
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_face_check;
    }



    GestureDetector.SimpleOnGestureListener listener = new GestureDetector.SimpleOnGestureListener() {

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

            if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {


                if (currentPosition + 1 < list.size()) {
                    currentPosition = currentPosition + 1;
                    tv_name.setText(list.get(currentPosition).studentName);
                    ImageLoaderHelper.getInstance().GlideImageLoader( FaceCheckActivity.this, list.get(currentPosition).faceImg, iv);
                } else {
                    Toast.makeText( FaceCheckActivity.this, "已经是最后一张了", Toast.LENGTH_SHORT).show();
                }

            } else if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {

                if (currentPosition - 1 >= 0) {
                    currentPosition = currentPosition - 1;
                    tv_name.setText(list.get(currentPosition).studentName);
                    ImageLoaderHelper.getInstance().GlideImageLoader( FaceCheckActivity.this, list.get(currentPosition).faceImg, iv);
                } else {
                    Toast.makeText( FaceCheckActivity.this, "已经是第一张了", Toast.LENGTH_SHORT).show();
                }

            }
            return false;
        }

    };

    @OnClick(R.id.btn_next)
    void btn_next() {
        if (currentPosition + 1 < list.size()) {
            currentPosition = currentPosition + 1;
            tv_name.setText(list.get(currentPosition).studentName);
            ImageLoaderHelper.getInstance().GlideImageLoader( FaceCheckActivity.this, list.get(currentPosition).faceImg, iv);
        } else {
            Toast.makeText( FaceCheckActivity.this, "已经是最后一张了", Toast.LENGTH_SHORT).show();
        }
    }

    @OnClick(R.id.btn_reset)
    void btn_reset() {
        Bundle bundle = new Bundle();
        if (type == 0) {
            bundle.putString("studentId", list.get(currentPosition).studentId);
        }else{
            bundle.putString("studentId", studentId);
        }
        bundle.putInt("faceType", 2);
        startActivity( FaceDetectorActivity.class, bundle);
        finish();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (type == 0) {
            return gd.onTouchEvent(event);
        } else {
          return  super.onTouchEvent(event);
        }
    }


    private void getStudentFace() {
        RestProxy.getInstance().getCollectStudentList("1", "",taskId, classId, new Observer<CollectStudentResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(CollectStudentResponse collectStudentResponse) {
                list = collectStudentResponse.data.pageInfo.list;
                if (list.size() > 0) {
                    currentPosition = 0;
                    tv_name.setText(list.get(0).studentName);
                    ImageLoaderHelper.getInstance().GlideImageLoader( FaceCheckActivity.this, list.get(0).faceImg, iv);
                }
            }

            @Override
            public void onError(Throwable e) {
                NetUtil.onError(e, FaceCheckActivity.this);
            }

            @Override
            public void onComplete() {

            }
        });
    }


}
